package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.PointCard;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 积分卡界面
 *
 * @author YQS
 */
@Deprecated
public class PointCardActivity extends TCBaseActivity implements View.OnClickListener {
    private ImageView ivCard;
    //全部2
    private TextView tvAllCardAmount;
    private RecyclerView recyclerView;
    private PointCardAdapter mAdapter;
    private List<PointCard> list;
    private SmartRefreshLayout refreshLayout;
    private boolean showLoading = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_activity_point_card);
        initView();
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
    }


    private void initView() {
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_stack_recharge_title));

        ivCard = (ImageView) findViewById(R.id.iv_card);
        tvAllCardAmount = (TextView) findViewById(R.id.tv_all_card_amount);
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setEnableAutoLoadMore(false);//开启自动加载功能（非必须）

        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                showLoading = false;
                loadData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore();
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        mAdapter = new PointCardAdapter(this, R.layout.talk_chain_item_point_card, list);
        recyclerView.setAdapter(mAdapter);
        loadData();

    }

    private void loadData() {
        list.clear();
        PointCard card = new PointCard.Builder().cardName(ResStringUtil.getString(R.string.open_planet_asset_point_silver_card)).score("26348").cardIcon(R.drawable.talk_chain_point_card_avatar2)
                .cardBackground(R.drawable.talk_chain_point_card_bg1).build();
        list.add(card);
        PointCard card1 = new PointCard.Builder().cardName(ResStringUtil.getString(R.string.open_planet_asset_point_silver_card)).score("26348").cardIcon("")
                .cardBackground("").build();
        list.add(card1);
        refreshLayout.finishRefresh();
        mAdapter.notifyDataSetChanged();


    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        }

    }

    private class PointCardAdapter extends CommonAdapter<PointCard> {
        RequestOptions options;

        public PointCardAdapter(Context context, int layoutId, List<PointCard> datas) {
            super(context, layoutId, datas);

            options = new RequestOptions()
                    .circleCrop().error(R.drawable.talk_chain_point_card_bg1).placeholder(R.drawable.talk_chain_point_card_bg1)
                    .transform(new RoundedCorners(5))
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }

        @Override
        protected void convert(ViewHolder holder, final PointCard bean, int position) {
            ImageView ivAvatar;
            TextView tvName;
            TextView tvPoint;
            ImageView ivBg;

            ivAvatar = holder.getView(R.id.iv_avatar);
            tvName = holder.getView(R.id.tv_name);
            tvPoint = holder.getView(R.id.tv_point);
            ivBg = holder.getView(R.id.iv_bg);

            tvName.setText(bean.getCardName());
            tvPoint.setText(bean.getScore());

            //
            Glide.with(PointCardActivity.this)
                    .load(bean.getCardIcon())
                    .apply(new RequestOptions().circleCrop().error(R.drawable.talk_chain_point_card_avatar2)
                            .placeholder(R.drawable.talk_chain_point_card_avatar2).diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(ivAvatar);

            Glide.with(PointCardActivity.this).load(bean.getCardBackgrond())
                    .apply(options)
                    .into(ivBg);

            holder.getView(R.id.itemLayout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PointCardActivity.this, PointCardDetailActivity.class);
                    intent.putExtra("title",bean.getCardName());
                    startActivity(intent);
                }
            });


        }
    }
}
